<!--  -->
<template>
  <div>
    <el-form>
      <el-form-item label="老人姓名">
        <el-input v-model="elderly.name"  style="width: 300px;" placeholder="请输入老人姓名"></el-input>
      </el-form-item>

      <el-form-item label="头像">

        <el-upload
    class="avatar-uploader"
    action="http://123.57.237.81:8080/caresystem/api/upload/add"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="elderly.photo" :src="'http://123.57.237.81:8080/caresystem/'+elderly.photo" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>

      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="elderly.gender">
      <el-radio :value="1" size="large">男</el-radio>
      <el-radio :value="0" size="large">女</el-radio>
    </el-radio-group>
      </el-form-item>
      <el-form-item label="生日">
        <el-date-picker
        v-model="elderly.birthday"
        type="date"
        value-format="YYYY-MM-DD"
        placeholder="请选择生日"
      />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang='ts' setup>
import { reactive,toRefs,ref,onMounted, inject} from 'vue'
import { Elderly } from '../../apis/elderly/type';
import { ElMessage, UploadProps } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'
import { CompressImage } from '../../utils/imgUtils';
const elderly=inject<Elderly>("elderly")!;


const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
   console.log(response);
   if(response.code==10000){
    elderly.photo=response.data.url;
   }
   
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  // if (rawFile.type !== 'image/jpeg') {
  //   ElMessage.error('Avatar picture must be JPG format!')
  //   return false
  // } else if (rawFile.size / 1024 / 1024 > 2) {
  //   ElMessage.error('Avatar picture size can not exceed 2MB!')
  //   return false
  // }

  let url = URL.createObjectURL(rawFile);
  return CompressImage(url,300);
  //return true
}



</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>